package com.alpaca.recommend.action;

import com.alpaca.recommend.service.ItemService;
import com.alpaca.recommend.util.Jackson2Util;
import com.alpaca.recommend.util.Response;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.spring.Inject;
import com.jfinal.plugin.spring.IocInterceptor;

/**
 * Created by Administrator on 2015/2/9.
 */
@Before(IocInterceptor.class)
public class ItemController extends Controller {

    @Inject.BY_TYPE
    private ItemService itemService;


    public void index() {
        this.render("item controller");
    }


    /**
     * 添加一个内容的动作.
     */
    public void add() {

        String itemId = getPara("itemId");
        String[] tag = getParaValues("tag");
        String content = getPara("content");
        String channelCode = getPara("channelCode");

        if (itemId == null) {
            renderText(Jackson2Util.toJson(Response.getFailedResponse("itemId不能为空")));
            return;
        }
        if (channelCode == null) {
            renderText(Jackson2Util.toJson(Response.getFailedResponse("频道号不存在")));
            return;
        }
        itemService.addItem(itemId, tag, content, channelCode);
        renderText("suc");
    }

    /**
     * 添加一个内容的动作.
     */
    public void del() {
        String itemId = getPara("itemId");

        if (itemId == null) {
            renderText(Jackson2Util.toJson(Response.getFailedResponse("itemId不能为空")));
            return;
        }
        itemService.delItem(itemId);
        renderText("suc");
    }

}
